import 'package:flutter/material.dart';

class FixedExtentListDemo extends StatelessWidget {
  const FixedExtentListDemo({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('FixedExtentListDemo'),
      ),
      body: ListView.builder(
        prototypeItem: const ListTile(title: Text('1')),
        itemBuilder: (context, index) {
          /// LayoutLogPrint是一个自定义组件，在布局时可以打印当前上下文中父组件给子组件的约束信息
          return LayoutLogPrint(
            tag: index,
            child: ListTile(
              title: Text("$index"),
            ),
          );
        },
      ),
    );
  }
}

class LayoutLogPrint<T> extends StatelessWidget {
  const LayoutLogPrint({
    super.key,
    this.tag,
    required this.child,
  });

  final Widget child;
  final T? tag; //指定日志tag

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (_, constraints) {
      // assert在编译release版本时会被去除
      assert(() {
        print('${tag ?? key ?? child}: $constraints');
        return true;
      }());
      return child;
    });
  }
}
